package com.sy.controller;

import com.github.pagehelper.Page;
import com.sy.dto.*;
import com.sy.entity.LeaveMessageDetail;
import com.sy.vo.NewLeavenWordsVo;
import com.sy.vo.NewReplyWordsVo;
import com.sy.vo.SearchLeaWordByKeyWordsVo;
import com.sy.vo.SearchTeaLeaWordByKeyVo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
public class NewMessageController extends BaseController{
    //添加留言
    @RequestMapping(value = "student",method = RequestMethod.POST,params = {"type=addLeaveWords"})
    public void addNewMessage(@RequestBody NewLeavenWordsVo newLeavenWordsVo){
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
        newLeavenWordsVo.setStudentId(String.valueOf(id));
        newLeavenWordsService.addNewMessage(newLeavenWordsVo);
    }

    //学生获取所有回复
    @RequestMapping(value = "student",method = RequestMethod.GET,params = {"type=getAllReplyMessage","leaveMessageId"})
    public List<LeaveMessageDetail> getAllReplyMessage(String leaveMessageId){
        System.out.println(leaveMessageId);
        return newLeavenWordsService.getAllReplyMessage(leaveMessageId);
    }

    //添加回复
    @RequestMapping(value = "student",method = RequestMethod.POST,params = {"type=addReplyWords"})
    public void addNewReplyMessage(@RequestBody NewReplyWordsVo newReplyWordsVo){
        String name = getSessionUser().getName();
        newReplyWordsVo.setName(name);
        newLeavenWordsService.addNewReplyWords(newReplyWordsVo);
    }

    //获取所有老师信息
    @RequestMapping(value = "student",method = RequestMethod.GET,params = {"type=getTeaInfo"})
    public TeaSimpleInfo getAllTeaSimpleInfo(){
        Integer sessionUserId = getSessionUserId();
        return teacherService.getTeaSimpleInfo(String.valueOf(sessionUserId));
    }

    //学生获取所有留言
    @RequestMapping(value = "student",method = RequestMethod.GET,params = {"type=getAllLeaveMessage","page","limit"})
    public Table getAllLeaveMessage(Integer page,Integer limit){
        SearchLeaWordByKeyWordsVo vo = new SearchLeaWordByKeyWordsVo();
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
        vo.setStudentId(id);
        vo.setLimit(limit);
        vo.setPage(page);
        Page<LeaveSimpleMessage> allLeaveMessage = newLeavenWordsService.getAllLeaveMessage(vo);
        return getTable(allLeaveMessage);
    }

    //学生获取所有留言带搜索关键字
    @RequestMapping(value = "student",method = RequestMethod.GET,params = {"type=getAllLeaveMessage","page","limit","title"})
    public Table getAllLeaveMessage(Integer page,Integer limit,String title) throws UnsupportedEncodingException {
        SearchLeaWordByKeyWordsVo vo = new SearchLeaWordByKeyWordsVo();
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
        vo.setStudentId(id);
        String content = URLDecoder.decode(title, "UTF-8");
        vo.setKeyWord(content);
        vo.setLimit(limit);
        vo.setPage(page);
        Page<LeaveSimpleMessage> allLeaveMessage = newLeavenWordsService.getAllLeaveMessage(vo);
        return getTable(allLeaveMessage);
    }

    //学生删除留言及其下属的回复
    @RequestMapping(value="student",method = RequestMethod.DELETE,params = {"type=deleteLeavenWord","levenWordId"})
    public boolean deleteLeavenWord(String levenWordId){
        System.out.println(levenWordId);
        return newLeavenWordsService.deleteLeavenWord(levenWordId);
    }

    //老师获取所有的留言
    @RequestMapping(value="teacher",method = RequestMethod.GET,params = {"type=getAllTeaLeaveMessage","page","limit"})
    public Table getAllTeaLeavenMessage(Integer page,Integer limit){
        Integer id = getSessionUserId();
        SearchTeaLeaWordByKeyVo vo = new SearchTeaLeaWordByKeyVo();
        vo.setTeacherId(id);
        vo.setLimit(limit);
        vo.setPage(page);
        Page<LeaveSimpleMessageTeacher> allLeaveMessage =  newLeavenWordsService.getAllLeaveMessageByTeacher(vo);;
        return getTable(allLeaveMessage);
    }

    //老师获取所有的留言带关键字
    @RequestMapping(value = "teacher",method = RequestMethod.GET,params = {"type=getAllTeaLeaveMessage","page","limit","title"})
    public Table getAllLeaveMessageByTeacher(Integer page,Integer limit,String title) throws UnsupportedEncodingException {
        SearchTeaLeaWordByKeyVo vo = new SearchTeaLeaWordByKeyVo();
        UserSession session = (UserSession) req.getSession().getAttribute("session");
        Integer id =  session.getId();
        vo.setTeacherId(id);
        String content = URLDecoder.decode(title, "UTF-8");
        vo.setKeyWord(content);
        vo.setLimit(limit);
        vo.setPage(page);
        Page<LeaveSimpleMessageTeacher> allLeaveMessage = newLeavenWordsService.getAllLeaveMessageByTeacher(vo);
        return getTable(allLeaveMessage);
    }

    //老师获取所有回复
    @RequestMapping(value = "teacher",method = RequestMethod.GET,params = {"type=getAllReplyMessage","leaveMessageId"})
    public List<LeaveMessageDetail> getAllReplyMessageTeacher(String leaveMessageId){
        System.out.println(leaveMessageId);
        return newLeavenWordsService.getAllReplyMessage(leaveMessageId);
    }

    //老师添加回复
    @RequestMapping(value = "teacher",method = RequestMethod.POST,params = {"type=addReplyWords"})
    public void addNewReplyMessageTeacher(@RequestBody NewReplyWordsVo newReplyWordsVo){
        String name = getSessionUser().getName();
        newReplyWordsVo.setName(name);
        newLeavenWordsService.addNewReplyWords(newReplyWordsVo);
    }
}
